require "engine.class"
local Mouse = require "engine.Mouse"
local DamageType = require "engine.DamageType"

module(..., package.seeall, class.make)

function _M:init(x, y, w, h, bgcolor)
	self.display_x = x
	self.display_y = y
	self.w, self.h = w, h
	self.bgcolor = bgcolor
	self.font = core.display.newFont("/data/font/VeraMono.ttf", 14)
	self.mouse = Mouse.new()
	self:resize(x, y, w, h)
end

--- Resize the display area
function _M:resize(x, y, w, h)
	self.display_x, self.display_y = x, y
	self.mouse.delegate_offset_x = x
	self.mouse.delegate_offset_y = y
	self.w, self.h = w, h
	self.font_h = self.font:lineSkip()
	self.font_w = self.font:size(" ")
	self.bars_x = self.font_w * 9
	self.bars_w = self.w - self.bars_x - 5
	self.surface = core.display.newSurface(w, h)
	self.texture, self.texture_w, self.texture_h = self.surface:glTexture()

	local tex_bg = core.display.loadImage("/data/gfx/ui/player-display.png")
	local tex_up = core.display.loadImage("/data/gfx/ui/player-display-top.png")
	local bw, bh = tex_bg:getSize()
	self.bg_surface = core.display.newSurface(w, h)
	local i = 0
	while i < h do
		self.bg_surface:merge(tex_bg, 0, i)
		i = i + bh
	end
	self.bg_surface:merge(tex_up, 0, 0)
end

-- Displays the stats
function _M:display()
	local player = game.player
	if not player or not player.changed then return self.surface end

	self.mouse:reset()
	local s = self.surface
	if self.bg_surface then
		s:erase(self.bgcolor[1], self.bgcolor[2], self.bgcolor[3])
		s:merge(self.bg_surface, 0, 0)
	else
		s:erase(self.bgcolor[1], self.bgcolor[2], self.bgcolor[3])
	end

	local cur_exp, max_exp = game.player.exp, game.player:getExpChart(game.player.level+1)

	local h = 6
	local x = 2
	s:drawColorStringBlended(self.font, player.name, 0, h, 0, 200, 255) h = h + self.font_h
	s:drawColorStringBlended(self.font, player.descriptor.element or "", 0, h, 0, 200, 255) h = h + self.font_h
	h = h + self.font_h
	
	s:drawColorStringBlended(self.font, "Level:   #00ff00#"..player.level, 0, h, 255, 255, 255) h = h + self.font_h
	s:erase(colors.DARK_GREEN.r, colors.DARK_GREEN.g, colors.DARK_GREEN.b, 255, self.bars_x, h, self.bars_w, self.font_h)
	s:erase(colors.GREEN.r, colors.GREEN.g, colors.GREEN.b, 255, self.bars_x, h, self.bars_w * cur_exp / max_exp, self.font_h)
	s:drawColorStringBlended(self.font, ("Exp:     #00ff00#%d/%d"):format(cur_exp, max_exp), 0, h, 255, 255, 255) h = h + self.font_h

	h = h + self.font_h

	s:erase(colors.VERY_DARK_RED.r, colors.VERY_DARK_RED.g, colors.VERY_DARK_RED.b, 255, self.bars_x, h, self.bars_w, self.font_h)
	if player.life > 0 then s:erase(colors.DARK_RED.r, colors.DARK_RED.g, colors.DARK_RED.b, 255, self.bars_x, h, self.bars_w * player.life / player.max_life, self.font_h) end
	s:drawColorStringBlended(self.font, ("#c00000#Life:    #ffffff#%d/%d"):format(player.life, player.max_life), 0, h, 255, 255, 255) h = h + self.font_h


	h = h + self.font_h
	s:drawColorStringBlended(self.font, ("STR: #00ff00#%3d    #ffffff#AC: #ffff00#%2d"):format(player:getStr(), player.combat_armor), 0, h, 255, 255, 255) h = h + self.font_h
	s:drawColorStringBlended(self.font, ("DEX: #00ff00#%3d    #ffffff#DF: #ffff00#%2d"):format(player:getDex(), math.floor(player.combat_def)), 0, h, 255, 255, 255) h = h + self.font_h
	s:drawColorStringBlended(self.font, ("CON: #00ff00#%3d"):format(player:getCon()), 0, h, 255, 255, 255) h = h + self.font_h
	h = h + self.font_h

	s:drawColorStringBlended(self.font, ("Resists: "), 0, h, 255, 255, 255) h = h + self.font_h
	if player.resists[DamageType.COLD] then s:drawColorStringBlended(self.font, ("#51ddff#  Cold:        %3d%%"):format(player.resists[DamageType.COLD]), 0, h, 255, 255, 255) h = h + self.font_h end
	if player.resists[DamageType.EARTH] then s:drawColorStringBlended(self.font, ("#8E4500#  Earth:       %3d%%"):format(player.resists[DamageType.EARTH]), 0, h, 255, 255, 255) h = h + self.font_h end
	if player.resists[DamageType.FIRE] then s:drawColorStringBlended(self.font, ("#ff0000#  Fire:        %3d%%"):format(player.resists[DamageType.FIRE]), 0, h, 255, 255, 255) h = h + self.font_h end
	if player.resists[DamageType.LIGHTNING] then s:drawColorStringBlended(self.font, ("#ffff00#  Lightning:   %3d%%"):format(player.resists[DamageType.LIGHTNING]), 0, h, 255, 255, 255) h = h + self.font_h end
	if player.resists[DamageType.MANA] then s:drawColorStringBlended(self.font, ("#DA70D6#  Mana:        %3d%%"):format(player.resists[DamageType.MANA]), 0, h, 255, 255, 255) h = h + self.font_h end
	if player.resists[DamageType.CHAOS] then s:drawColorStringBlended(self.font, ("#00ff00#  Chaos:       %3d%%"):format(player.resists[DamageType.CHAOS]), 0, h, 255, 255, 255) h = h + self.font_h end
	h = h + self.font_h

   if savefile_pipe.saving then h = h + self.font_h s:drawColorStringBlended(self.font, "#YELLOW#Saving...", 0, h, 255, 255, 255) h = h + self.font_h end

	s:updateTexture(self.texture)
	return s
end

function _M:toScreen()
	self:display()
	self.texture:toScreenFull(self.display_x, self.display_y, self.w, self.h, self.texture_w, self.texture_h)
end
